package com.yl.ydk.presenter.impl

import android.content.Context
import android.content.Intent
import androidx.recyclerview.widget.LinearLayoutManager
import com.yl.ydk.R
import com.yl.ydk.api.Api
import com.yl.ydk.base.BasePresenter
import com.yl.ydk.bean.TestPapersBean
import com.yl.ydk.presenter.contract.TestPapersContract
import com.yl.ydk.ui.activity.HealthtestQuestionActivity
import com.yl.ydk.ui.adapter.TestPapersAdapter
import okhttp3.RequestBody
import rx.Observer
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers

class TestPapersImpl : BasePresenter<TestPapersContract.View>(),
    TestPapersContract.Presenter<TestPapersContract.View> {

    private var adapter: TestPapersAdapter? = null
    private var listData = mutableListOf<TestPapersBean.DataX>()

    override fun getTestPapers(context: Context, body: RequestBody?, loadMore: Boolean) {
        showLoadingDialog(context)
        addSubscrebe(
            Api.getInstance()?.getTestPapers(body)
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribeOn(Schedulers.io())
                ?.unsubscribeOn(Schedulers.io())
                ?.subscribe(object : Observer<TestPapersBean?> {
                    override fun onCompleted() {}
                    override fun onError(e: Throwable) {
                        dismissLoadingDialog()
                        mView?.showError(e.toString())
                    }

                    override fun onNext(t: TestPapersBean?) {
                        dismissLoadingDialog()
                        if (t?.data != null && t.code == 1) {
                            if (loadMore) {
                                adapter?.let {
                                    t.data.list.forEach {
                                        listData.add(it)
                                    }
                                    it.notifyDataSetChanged()
                                    mView?.getSmartRefreshLayout()?.finishLoadMore()
                                }
                            } else {
                                mView?.getRvTest()?.let {
                                    adapter = null
                                    listData.clear()
                                    listData = t.data.list.toMutableList()
                                    adapter = TestPapersAdapter(listData)
                                    val manager = LinearLayoutManager(context)
                                    it.adapter = adapter
                                    it.layoutManager = manager
                                    adapter?.onListItemClick(object :
                                        TestPapersAdapter.onListItemClick {
                                        override fun onListItemClick(item: TestPapersBean.DataX?) {
                                            mView?.getActivity()?.let {
                                                val intent =
                                                    Intent(
                                                        it,
                                                        HealthtestQuestionActivity::class.java
                                                    )
                                                intent.putExtra("testId", item?.question_id)
                                                it.startActivityIn(intent, it)
                                            }
                                        }
                                    })
                                    mView?.getSmartRefreshLayout()?.finishRefresh()
                                }
                            }
                        } else {
                            mView?.showError(context.getString(R.string.connectFailure))
                        }
                    }
                })
        )
    }
}